package com.ssm.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ssm.auth.application.convert.AuthRolePermissionDtoConvert;
import com.ssm.auth.application.dto.AuthRolePermissionDTO;
import com.ssm.auth.entity.Result;
import com.ssm.auth.domain.bo.AuthRolePermissionBO;
import com.ssm.auth.domain.service.AuhtRolePermissionDomainService;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rolePermission")
@Log4j2
public class RolePermissionController {

    @Resource
    private AuhtRolePermissionDomainService auhtRolePermissionDomainService;

    /**
     * 新增角色权限
     * @param authRolePermissionDTO
     * @return
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if(log.isInfoEnabled()) {
                log.info("RolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限id不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色id不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDtoConvert.INSTANCE.AuthRolePermissionDtoToBo(authRolePermissionDTO);
            return Result.ok(auhtRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            log.info("RolePermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限失败");
        }
    }
}
